<?php

namespace app\controllers\wx;

use app\models\ActivityZan;
use app\models\WeUser;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\web\NotFoundHttpException;
use yii\web\UploadedFile;

class PageController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    private $we_user = null;
    private $we_app = null;

    public function beforeAction($action)
    {
//        if( !Yii::$app->wechat->isAuthorized()) {
//            return Yii::$app->wechat->authorizeRequired()->send();
//        }
//        $this->we_user = Yii::$app->wechat->user;
//        $this->we_app =  Yii::$app->wechat->app;
//
//        $user = new WeUser();//微信登录自动保存用户信息
//        $user->attributes = $this->we_user->original;
//        $user->save();

        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    public function actionIndex()
    {
        return $this->renderPartial('new');
    }

    public function actionCreate()
    {
        $model = new ActivityZan();

        if($model->load(Yii::$app->request->post())) {
            $model->file = UploadedFile::getInstance($model, 'file');
            $model->file->saveAs('upload/' . $model->file->baseName . '.' . $model->file->extension,false);
            $model->image = '/upload/' . $model->file->baseName . '.' . $model->file->extension;
            $model->belong = $this->we_user->openid;
            $model->belong = 1;
        }

        //var_dump($model->load(Yii::$app->request->post()));die;

        if ( $model->save()) {
            //var_dump($model);die;
            return $this->redirect(['activity', 'id' => $model->id]);
        } else {
            //var_dump($model->errors);die;
            return $this->render('../activity-zan/create', [
                'model' => $model,
            ]);
        }
    }

    public function actionActivity($id)
    {
        $model = new ActivityZan();

        return $this->renderPartial('activity', [
            'model' => $this->findActivity($id),
        ]);
    }

    protected function findActivity($id)
    {
        if (($model = ActivityZan::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
